package ads_api

import (
	"server/global"
	"server/model/common/request"
	"server/model/common/response"
	"server/utils"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type AdsApiApi struct {
}

var adsApiService = AdsApiService{}

func (adsApiApi *AdsApiApi) CreateAdsApi(c *gin.Context) {
	var adsApi AdsApi
	err := c.ShouldBindJSON(&adsApi)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	adsApi.CreatedBy = utils.GetUserID(c)
	if err := adsApiService.CreateAdsApi(&adsApi); err != nil {
		global.Logger.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

func (adsApiApi *AdsApiApi) DeleteAdsApi(c *gin.Context) {
	var adsApi AdsApi
	err := c.ShouldBindJSON(&adsApi)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	adsApi.DeletedBy = utils.GetUserID(c)
	if err := adsApiService.DeleteAdsApi(adsApi); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

func (adsApiApi *AdsApiApi) DeleteAdsApiByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	deletedBy := utils.GetUserID(c)
	if err := adsApiService.DeleteAdsApiByIds(IDS, deletedBy); err != nil {
		global.Logger.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

func (adsApiApi *AdsApiApi) UpdateAdsApi(c *gin.Context) {
	var adsApi AdsApi
	err := c.ShouldBindJSON(&adsApi)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	adsApi.UpdatedBy = utils.GetUserID(c)
	if err := adsApiService.UpdateAdsApi(adsApi); err != nil {
		global.Logger.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

func (adsApiApi *AdsApiApi) FindAdsApi(c *gin.Context) {
	var adsApi AdsApi
	err := c.ShouldBindQuery(&adsApi)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if readsApi, err := adsApiService.GetAdsApiById(adsApi.ID); err != nil {
		global.Logger.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"readsApi": readsApi}, c)
	}
}

func (adsApiApi *AdsApiApi) GetAdsApiList(c *gin.Context) {
	var pageInfo AdsApiSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := adsApiService.GetAdsApiInfoList(pageInfo); err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func (adsApiApi *AdsApiApi) GetAllAdsApis(c *gin.Context) {
	adsApi, err := adsApiService.GetAllAdsApis()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"adsApi": adsApi}, "获取成功", c)
	}
}

func (adsApiApi *AdsApiApi) GetAdsApi(c *gin.Context) {
	adsApi, err := adsApiService.GetAdsApi()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"adsApi": adsApi}, "获取成功", c)
	}
}
